﻿using Assets.Script.Data.EnemyData;
using Assets.Script.Data.Player;
using Assets.Script.HelpSystem.AtkSystem.AtkWays;
using Assets.Script.HelpSystem.AtkSystem.Base;
using Assets.Script.HelpSystem.AtkSystem.Config;
using Assets.Script.HelpSystem.SaveSystem;
using Assets.Script.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Assets.Script.HelpSystem.AtkSystem;
using Assets.Script.Human.Player.Config;
using Script.NetSystem.Core;
using Script.UiSystem;
using UnityEditor;
using UnityEngine;

namespace Assets.Script.Human.Player
{
    [RequireComponent(typeof(Rigidbody))]
    public class PlayerBody: MonoBehaviour
    {
        public int Id { get; set; } = -1;

        public Transform fire_root, fire_forward, fire_back, fire_right, fire_left;

        public Animator animator;

        //[HideInInspector]
        public PlayerState PlayerState = new PlayerState();
        [HideInInspector] public PlayerData PlayerData { get; set; } = new PlayerData();
        [HideInInspector] public AtkInfo[] AtkInfos { get; set; }
        [HideInInspector] public Rigidbody Rigidbody { get; set; }

        [HideInInspector] public PlayerBuffManager BuffManager;

        //[HideInInspector]
        public float NowHp { get;private set; } = 0;
        //[HideInInspector]
        public float NowMana { get; private set; } = 0;

        public void Awake()
        {
            Rigidbody = GetComponent<Rigidbody>();

            //初始化一些参数


            if (SaveManager.saveData != null)
            {
                var hd = SaveManager.saveData.HeroConfig.GetHeroData_Copy(SaveManager.saveData.HeroName);
                PlayerData = hd.playerData;
                AtkInfos = hd.atkInfos;
            }
            else//测试使用数据
            {
                AtkInfos = new AtkInfo[] { AtkInfoDic.GetInfo("ShootNormal"), AtkInfoDic.GetInfo("GunS"), AtkInfoDic.GetInfo("Healing") };
                PlayerData.AttackDir = new AttackDir() { forward = 1, back = 0, left = 0, rigt = 0 };
                PlayerData.ShiedRecTime = new BaseData(3);
                PlayerData.MaxHp = new BaseData(100);
                PlayerData.MaxMana = new BaseData(50);
                PlayerData.RunSpeed = new BaseData(4);
                PlayerData.AtkSpeed = new BaseData(2);
                PlayerData.InjuredInterval = new BaseData(0.5f);

                PlayerData.BulletData = new BulletData()
                {
                    Atk = new BaseData(3),
                    BulletScale = new BaseData(1),
                    BulletLifeTime = new BaseData(3),
                    BulletSpeed = new BaseData(10)
                };
            }


            NowHp = PlayerData.MaxHp;
            NowMana = PlayerData.MaxMana;

            BuffManager = gameObject.AddComponent<PlayerBuffManager>();
            BuffManager.Init(this);


              //实例化武器
            Instantiate(Resources.Load<GameObject>(ResourcePath.Fyers+PlayerData.WeaponName), transform);
            //----------------UI界面
            /*
            //todo 判断是本机
            //初始化相机
            var sfh = Camera.main.gameObject.GetComponent<ScriptFindHelp>();
            sfh.CameraFllow.Init(transform);
            //初始化UI
            sfh.AtkBarManager.GetComponent<AtkBarManager>().Init(this);
            sfh.StateBlock.Init(this);
            //*****************
            */
        }

        public NetPlayerController InitLocalPlayer(RoleData roleData)
        {
            
            //初始化数据
            Id = roleData.id;
            PlayerData = roleData.playerData;
            AtkInfos = roleData.atkInfos;

            AtkManager atkManager = new AtkManager(this);
            PlayerActionManager actionManager = gameObject.AddComponent<PlayerActionManager>();
            NetPlayerController netPlayerController = gameObject.AddComponent<NetPlayerController>();
            
            netPlayerController.Init(actionManager, this, atkManager, GlobalCache.PlayerNetId == Id);
            
            if (GlobalCache.PlayerNetId == Id)
            {
                gameObject.AddComponent<LocalPlayerController>().Init(this, actionManager, atkManager,true);
            }
            
            return netPlayerController;
            
        }
        
        public void TakeDamage(float dmg,float force,Vector3 position)
        {
            NowHp -= dmg;
            EventManager.Broadcast(EventName.Player.HpChange, Id, -dmg, NowHp);
        }

        /// <summary>
        /// 网络
        /// </summary>
        /// <param name="mp"></param>
        public void SetHp(float hp)
        {
            NowHp = hp;
        }
        /// <summary>
        /// 网络
        /// </summary>
        /// <param name="mp"></param>
        public void SetMp(float mp)
        {
            NowHp = mp;
        }

        public void GetHeal(float heal)
        {
            NowHp += heal;
            if (NowHp>PlayerData.MaxHp)
            {
                NowHp = PlayerData.MaxHp;
            }
            EventManager.Broadcast(EventName.Player.HpChange, Id, heal, NowHp);
        }

        public bool UseMana(float cost)
        {
            if (NowMana>=cost)
            {
                NowMana -= cost;
                EventManager.Broadcast(EventName.Player.ManaChange, Id, cost, NowMana);
                return true;
            }
            else
            {
                return false;
            }
            
        }

        public void GetMana(float value)
        {
            NowMana += value;
            if (NowMana > PlayerData.MaxMana)
            {
                NowMana = PlayerData.MaxMana;
            }
            EventManager.Broadcast(EventName.Player.ManaChange, Id, value, NowMana);
        }

    }
}
